package com.yueke.gemini.snake;

import java.awt.*;
import java.awt.event.*;

/**
 * 游戏菜单
 */
public class Yard  extends Frame implements ActionListener {

    /**
     * @param args
     */
    public static final int ROWS =30;
    public static final int COLS =30;
    public static final int BLOCK_SIZE =15;

    private MenuBar menubar;
    private Menu Option;
    private MenuItem startItem,pauseItem,continueItem,exitItem;


    @SuppressWarnings("unused")
    private Font fontGameOver = new Font("宋体", Font.BOLD,50);

    private int score =0;
    Snake s =new Snake(this);
    PaintThread paintThread =new PaintThread();
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Yard y =new Yard();
        y.lunch();
    }
    private void lunch(){
        this.setLocation(200,200);
        this.setSize(COLS * BLOCK_SIZE,ROWS* BLOCK_SIZE);
        this.setBackground(Color.black);
        this.setTitle("贪吃蛇游戏");
        this.addWindowListener(new WindowAdapter(){

            public void windowClosing(WindowEvent e) {
                // TODO Auto-generated method stub
                System.exit(0);
            }

        });
        this.setResizable(false);
        this.setVisible(true);
        //new Thread(paintThread).start();
        //this.addKeyListener(new Keykess());
        this.addKeyListener(new Keykess());
        menubar = new MenuBar();
        Option = new Menu("选项");
        startItem = new MenuItem("开始");
        pauseItem = new MenuItem("暂停");
        continueItem = new MenuItem("继续");
        exitItem = new MenuItem("退出");

        Option.add(startItem);
        Option.add(pauseItem);
        Option.add(continueItem);
        Option.add(exitItem);
        menubar.add(Option);

        this.setMenuBar(menubar);
        startItem.addActionListener(this);
        pauseItem.addActionListener(this);
        continueItem.addActionListener(this);
        exitItem.addActionListener(this);
    }

    public class PaintThread implements Runnable {
        boolean pause = false;
        public void run(){
            while(true){
                if(pause) continue;
                else repaint();

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        public void pause() {
            // TODO Auto-generated method stub
            this.pause = true;
        }

        public void continueGame() {
            // TODO Auto-generated method stub
            this.pause = false;
        }
    }
    public class Keykess extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub
            int Key = e.getKeyCode();

            if(Key== KeyEvent.VK_F1){
                new Thread(paintThread).start();
            }
            if(Key== KeyEvent.VK_SPACE){
                paintThread.pause();
            }
            if(Key== KeyEvent.VK_ENTER){
                paintThread.continueGame();
            }

//            s.keyPressed(e);
        }
    }




    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        Color c = g.getColor();
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, COLS*BLOCK_SIZE, ROWS*BLOCK_SIZE);
        g.setColor(Color.black);
        for(int i=1;i<ROWS;i++){
            g.drawLine(0, BLOCK_SIZE*i, COLS*BLOCK_SIZE, BLOCK_SIZE*i);
        }
        for(int i=1;i<COLS;i++){
            g.drawLine( BLOCK_SIZE*i,0,  BLOCK_SIZE*i, ROWS*BLOCK_SIZE);
        }
        g.setColor(Color.blue);
        g.drawString("分数："+score, 20, 60);
        g.setColor(c);
        s.draw(g);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==startItem){
            new Thread(paintThread).start();
        }
        if(e.getSource()==pauseItem){
            paintThread.pause();
        }
        if(e.getSource()==continueItem){
            paintThread.continueGame();
        }
        if(e.getSource()==exitItem){
            System.exit(0);
        }

    }
}

